/**
 * 
 */
package framework.system.service.imp;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Service;

import framework.core.dao.IBaseDao;
import framework.core.util.Kit;
import framework.system.entity.SysUser;
import framework.system.service.IUserService;
/**
 * @ClassName: UserService 
 * @Description: 用户管理Service实现
 * @author: Ching Wang
 * @date 2013-5-9 下午4:48:09 
 *
 */
@Service
public class UserService implements IUserService {

	@Resource private IBaseDao<SysUser> userDao;
	
	/* (非 Javadoc) 
	 * <p>Title: getUser</p> 
	 * <p>Description: </p> 
	 * @param userId
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IUserService#getUser(java.lang.String) 
	 */
	@Override
	public SysUser getUser(String userId) throws Exception {
		return userDao.findById(SysUser.class, userId);
	}

	/* (非 Javadoc) 
	 * <p>Title: saveOrUpdate</p> 
	 * <p>Description: </p> 
	 * @param sysUser
	 * @throws Exception 
	 * @see framework.system.service.IUserService#saveOrUpdate(framework.system.entity.SysUser) 
	 */
	@Override
	public void saveOrUpdate(SysUser sysUser) throws Exception {
		if(Kit.isNotNull(sysUser.getId())){
			sysUser.setModifyDate(new Date());
			userDao.update(sysUser);
		}
		else{
			sysUser.setCreateDate(new Date());
			sysUser.setPassword(Kit.getMd5("111111"));
			userDao.save(sysUser);
		}
	}

	/* (非 Javadoc) 
	 * <p>Title: listUser</p> 
	 * <p>Description: </p> 
	 * @param criteria
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IUserService#listUser(org.hibernate.criterion.DetachedCriteria, java.lang.String, java.lang.String) 
	 */
	@Override
	public List<SysUser> listUser(DetachedCriteria criteria, String page, String rows) throws Exception {
		int limit = Kit.isNotNull(rows) ? Integer.parseInt(rows) : 0;
		int start = Kit.isNotNull(page) ? ((Integer.parseInt(page)-1) * limit) : 0;
		return userDao.findByCriteria(criteria, start, limit);
	}

	/* (非 Javadoc) 
	 * <p>Title: getCount</p> 
	 * <p>Description: </p> 
	 * @param criteria
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IUserService#getCount(org.hibernate.criterion.DetachedCriteria) 
	 */
	@Override
	public long getCount(DetachedCriteria criteria) throws Exception {

		return userDao.getCount(criteria);
	}

	/* (非 Javadoc) 
	 * <p>Title: delUser</p> 
	 * <p>Description: </p> 
	 * @param userId
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IUserService#delUser(java.lang.String) 
	 */
	@Override
	public boolean delUser(String[] userIds) throws Exception {
		boolean flag = true;
		for(String uid : userIds){
			SysUser sysUser = userDao.findById(SysUser.class, uid);
			if(sysUser != null){
				flag &= userDao.delete(sysUser);
			}
		}
		
		return flag;
	}

	/* (非 Javadoc) 
	 * <p>Title: findByCriteria</p> 
	 * <p>Description: </p> 
	 * @param detachedCriteria
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IUserService#findByCriteria(org.hibernate.criterion.DetachedCriteria) 
	 */
	@Override
	public List<SysUser> findByCriteria(DetachedCriteria detachedCriteria)
			throws Exception {
		return userDao.findByCriteria(detachedCriteria);
	}
}
